#ifndef IMPORTFUNCTIONS_HPP
#define IMPORTFUNCTIONS_HPP

#include "meshTypes_fwd.hpp"
#include "settingsImport.hpp"
#include "settingsFunctions.hpp"
#include "sweSolverTypes_fwd.hpp"
#include "vtkExport.hpp"
#include "vtkImport.hpp"
#include "vtkFunctions.hpp"
#include "vtkFunctions_import.hpp"

#include "tinyxml2.h"

#include <filesystem>
#include <string_view>

namespace hms
{
namespace fs = std::filesystem;
namespace xml = tinyxml2;


fs::path getVtkFilePath(
	bool resumeSimulation,
	const fs::path& caseDir,
	const std::string caseName
);

void setVtkFilePath(Settings&);


void importAndOptimiseMesh(
	MeshVariant& meshVar,
	ImportSettings& importSettings
);


void createBackup( const fs::path&, std::string_view, bool dryRun );


void updateOrCreateDomainFile(
	Settings& s,
	xml::XMLDocument* domainDoc,
	const MeshVariant& meshVar
);

void updateOrCreateBoundaryFile(
	Settings& s,
	xml::XMLDocument* boundaryDoc,
	const MeshVariant& meshVar
);

MeshVariant readMesh(
	Settings& s,
	xml::XMLDocument* domainDoc,
	xml::XMLDocument* boundaryDoc
);

auto  readMesh( Settings& ) -> MeshVariant;

void writeMesh( Settings&, const MeshVariant& );

auto   getMesh( Settings& ) -> MeshVariant;



template<typename SolverType>
void readFields(
	Settings& s,
	SolverBase<SolverType>& solver,
	const xml::XMLDocument* domainDoc,
	const xml::XMLDocument* boundaryDoc
);


using RasterItem = ImportSettings::ImportItem<ImportSettings::RasterFormat>;
ArrayXXs importRaster( const RasterItem&, const MeshVariant& );


std::optional<ArrayXXs>   readDEM      ( Settings&, const MeshVariant& );

std::optional<ArrayXXs>   readRoughness( Settings&, const MeshVariant& );

std::optional<TimeSeries> readRain     ( Settings&, const MeshVariant& );


void writeDEM(
	ArrayXXs&& zValues,
	Settings& settings,
	const MeshVariant& meshVar,
	xml::XMLDocument* domainDoc,
	xml::XMLDocument* boundaryDoc
);

void writeRoughness(
	ArrayXXs&& fieldVals,
	Settings& settings,
	const MeshVariant& meshVar
);

void writeRain(
	TimeSeries&& roughnessVals,
	Settings& settings,
	const MeshVariant& meshVar
);

void importDEM(
	Settings&,
	const MeshVariant&,
	xml::XMLDocument* domainDoc,
	xml::XMLDocument* boundaryDoc
);

void importRoughness( Settings&, const MeshVariant& );

void importRain     ( Settings&, const MeshVariant& );

void importData(Settings&);

} // namespace hms

#include "importFunctions.tpp"

#endif